import { Notify } from 'quasar'
/**
 * httpMixin全局混入
 * 如果需要则取消下面的注释以及最下一行的注释（同时取消types/main.d.ts的关于http的类型的注释）
 * 但是不建议使用全局混入
 * 可能存在同名以及浪费性能等未知风险
 */
// import HttpMixin from '@/mixins/httpMixin'

export default ({ Vue }: { Vue: MgVue }) => {
    Vue.mixin({
        //  添加vue支持的属性及方法( methods | components | directives | created | mounted .....)
        //  !!!强烈不建议在这里声明vue生命周期钩子(因为在每个组件中执行)
        methods: {
            goBack (this: MgVue): void {
                this.$router.go(-1)
            },
            _isValid (this: MgVue, obj: any, propName: any) {
                if (obj === null || obj === undefined) {
                    return false
                }

                // Object or Array
                if (obj instanceof Object) {
                    return propName ? (propName in obj) : true
                } else if (obj instanceof Array) {
                    return !!obj.length
                } else { // 这里返回true(包含了 obj === 0 的情况)
                    return true
                }
            },
            showNotify (this: MgVue, config: any) {
                !('timeout' in config) && (config.timeout = 1000)
                !('position' in config) && (config.position = 'bottom')
                !('color' in config) && (config.color = 'grey')
                Notify.create(config)
            },
            showNotifyByCode (notifyConfig: {
                code: string,
                timeout?: number,
                position?: string,
                color?: string,
                message?: string
            } = { code: 'success' }) { // code：success fail
                const { code, ...config } = (notifyConfig || {})
                !('timeout' in config) && (config.timeout = 2000)
                !('position' in config) && (config.position = 'bottom')
                if (code === 'success') {
                    config.color = 'green'
                    config.message = '操作成功！'
                } else if (code === 'fail') {
                    config.color = 'red'
                    !config.message && (config.message = '操作失败！')
                } else {
                    !config.color && (config.color = 'grey')
                }
                Notify.create(config)
            },        
            responseValidate (this: MgVue, response: any): boolean { // 验证http接口返回的response
                if (response.code === 'fail') {
                    if ('showNotify' in this) {
                        this.showNotify({
                            message: 'request failed: ' + response.info,
                            position: 'center',
                            color: 'red'
                        })
                    }
                    return false
                }

                return true
            },
            download (this: MgVue, response: any) {
                if (!response) {
                    return
                }

                let fileName = 'test.txt'
                if ('headers' in response &&
                    'content-disposition' in response.headers &&
                    response.headers['content-disposition'].indexOf('filename') > -1) {
                    const header = response.headers['content-disposition'].split(';')
                    for (let i = 0; i < header.length; i++) {
                        if (header[i].trim().startsWith('filename')) {
                            const arr = header[i].split('=')
                            fileName = arr[arr.length - 1].trim()
                            break
                        }
                    }
                }
                let url = window.URL.createObjectURL(new Blob([response.data]))
                let link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                link.setAttribute('download', decodeURI(fileName))
                link.click()
            }
        }
    })

    // Vue.mixin(HttpMixin)
}

/*
* 以上所有方法在组件中直接调用, 都不需要import, 因为它们已注册到每个组件
*
* 例如:
*   this.responseValidate(response)
*
* */
